//
//  XWHACommonPickPopupContentView.swift
//  XiaoweHearingAid
//
//  Created by Riven on 2023/3/15.
//

import UIKit

class XWHACommonPickPopupContentView: AppOverlayerPopupContentView & UIPickerViewDelegate & UIPickerViewDataSource {

    lazy var pickerView = UIPickerView()
    
    lazy var pickerItems: [String] = []
    
    private var pickerViewHeight: Double = 162
    private var sIndex: Int = 0
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    init(pickerViewHeight: Double) {
        self.pickerViewHeight = pickerViewHeight

        super.init(frame: .zero)
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func addSubViews() {
        super.addSubViews()
        
        addSubview(pickerView)
        
        bringCancelConfirmBtnToFront()
        
        titleLb.textColor = majorTextColor
        titleLb.font = AppFont.harmonyOSSans(ofSize: 20, weight: .bold)
        titleLb.textAlignment = .center
        
//        configTopBottomCancelConfirmBtn()
        configLeftRightCancelConfirmBtn()

        pickerView.delegate = self
        pickerView.dataSource = self
        
        titleLb.isHidden = true
    }
    
    override func relayoutSubViews() {
        pickerView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(22)
            make.top.equalToSuperview().offset(44)
            make.height.equalTo(pickerViewHeight)
        }
//        relayoutTopBottomCancelConfirmBtn()
        relayoutLeftRightCancelConfirmBtn()
    }
    
    func update(pickerItems: [String], sIndex: Int) {
        self.pickerItems = pickerItems
        self.sIndex = sIndex
        pickerView.reloadAllComponents()
        pickerView.selectRow(sIndex, inComponent: 0, animated: false)
    }
    
    // MARK: - UIPickerViewDelegate & UIPickerViewDataSource
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return pickerItems.count
    }
    
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        let rLabel = view as? UILabel
        lazy var cLabel = UILabel()
        if let tLabel = rLabel {
            cLabel = tLabel
        }
        cLabel.font = AppFont.harmonyOSSans(ofSize: 32, weight: .medium)
        cLabel.textColor = majorTextColor
        cLabel.textAlignment = .center
        
        cLabel.text = pickerItems[row]
        
        return cLabel
    }
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 54
    }
    
}
